package com.queen.restaurant.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.queen.restaurant.base.BaseMessage;
import com.queen.restaurant.domain.CourseType;
import com.queen.restaurant.domain.Restaurant;
import com.queen.restaurant.domain.Table;
import com.queen.restaurant.domain.User;
import com.queen.restaurant.service.CourseTypeService;
import com.queen.restaurant.service.RestaurantService;
import com.queen.restaurant.service.TableService;
import com.queen.restaurant.util.BaiduGeoUtil;
import com.queen.restaurant.util.HttpClientUtil;

/**
 * 餐厅操作Controller
 * 
 * @author Queen Description: <br/>
 *         Program Name:QueenRestaurant <br/>
 *         Date:2013.07.11 <br/>
 *         重新整理
 * */

@Controller
@RequestMapping("/restaurant")
public class RestaurantController {

	@Autowired
	private RestaurantService restaurantService;

	@Autowired
	private TableService tableService;

	@Autowired
	private CourseTypeService courseTypeService;

	// @ModelAttribute("restaurant")
	// public Restaurant getRestaurant() {
	// return new Restaurant();
	// }

	// 获取餐厅注册页面
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String getRegisterPage() {
		return "restaurant/register";
	}

	// 新增餐厅
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public String restaurantRegister(@RequestBody Restaurant restaurant,
			HttpSession session) {
		restaurantService.createRestaurant(restaurant);

		String baiduResult = craeteNewRestaurantPoi(restaurant);
		String restaurantPoiId = BaiduGeoUtil.createPoiResult(baiduResult);
		if (!"error".equals(restaurantPoiId)) {
			restaurant.setRestaurantPoiId(restaurantPoiId);
			restaurantService.updateRestaurant(restaurant);
			session.setAttribute("restaurant", restaurant);
		} else {
			// 抛出异常,回滚事件
		}

		return "restaurant/manage";
	}

	// 登陆
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView restaurantLogin(
			@RequestParam("restaurantName") String restaurantName,
			@RequestParam("restaurantPassword") String restaurantPassword,
			HttpSession session) {
		ModelAndView mav = new ModelAndView();
		String viewName = "index";
		Restaurant restaurant = restaurantService.restaurantLogin(
				restaurantName, restaurantPassword);
		if (restaurant != null) {
			System.out.println("restaurant != null");
			viewName = "restaurant/restaurant_manage";
			session.setAttribute("restaurant", restaurant);
		}
		mav.addObject(restaurant);
		mav.setViewName(viewName);
		return mav;
	}

	// 餐厅信息
	@RequestMapping(value = "/info")
	public ModelAndView restaurantInfo(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
		mav.addObject("restaurant", restaurant).setViewName(
				"restaurant/restaurant_info");
		return mav;
	}

	// 修改餐厅信息
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Restaurant updateRestaurant(HttpSession session,
			@RequestBody Restaurant restaurant) {
		restaurantService.updateRestaurant(restaurant);
		session.setAttribute("restaurant", restaurant);
		return restaurant;
	}

	// 扫描餐桌ID获取餐桌
	@RequestMapping(value = "/get_restaurant_byId", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getRestaurantById(
			@RequestParam("restaurantId") String restaurantId) {
		Restaurant restaurant = null;
		int result = BaseMessage.ERROR;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		restaurant = restaurantService.getRestaurantById(restaurantId);
		
		if (restaurant != null) {
			result = BaseMessage.SUCCESS;
		}
		resultMap.put("result", result);
		resultMap.put("restaurant", restaurant);
		return resultMap;
	}

	// 获取餐厅注册页面
	@RequestMapping(value = "/manage")
	public String getRestaurantManagePage() {
		return "restaurant/restaurant_manage";
	}

	// 创建用户POI数据
	public String craeteNewRestaurantPoi(Restaurant restaurant) {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("geotable_id", BaiduGeoUtil.RESTAURANT_POI_GEOTABLE_ID);
		paramMap.put("ak", BaiduGeoUtil.AK);
		paramMap.put("coord_type", 3);
		paramMap.put("longitude", restaurant.getLongitude());
		paramMap.put("latitude", restaurant.getLatitude());

		paramMap.put("title", restaurant.getRestaurantName());
		paramMap.put("address", restaurant.getAddress());
		paramMap.put("restaurant_id", restaurant.getId());
		paramMap.put("restaurant_img_url", restaurant.getRestaurantImgUrl());
		paramMap.put("restaurant_tel", restaurant.getPhone());

		// paramMap.put("tags", "");
		return HttpClientUtil.getHttpPostResult(BaiduGeoUtil.CREATE_POI_URL,
				paramMap);
	}

	// // 获取餐厅的所有餐桌
	// @RequestMapping(value = "/tables")
	// public ModelAndView getTables(HttpSession session) {
	// ModelAndView mav = new ModelAndView();
	// String viewName = "restaurant/restaurant_tables";
	// mav.setViewName(viewName);
	// return mav;
	// }

	// // 餐厅的菜品类别
	// @RequestMapping(value = "/course_type")
	// public ModelAndView getCourseType(HttpSession session) {
	// ModelAndView mav = new ModelAndView();
	// Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
	//
	// List<CourseType> courseTypes =
	// courseTypeService.getCourseTypeByRestaurantId(restaurant.getId());
	// // List<CourseType> courseTypes = restaurant.getCourseTypes();
	// mav.addObject("courseTypes", courseTypes)
	// .setViewName("restaurant/restaurant_course_type");
	// return mav;
	// }
	//
	// @RequestMapping(value = "/tableList")
	// @ResponseBody
	// public List<Table> getTableList(HttpSession session){
	// Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
	// List<Table> tableList =
	// tableService.getTablesByRestaurantId(restaurant.getId());
	// return tableList;
	// }

	// //删除餐桌
	// @RequestMapping(value="/table_delete")
	// @ResponseBody
	// public List<Table> deleteTable(
	// @RequestBody String tableId, HttpSession session){
	// System.out.println("delete_table");
	// System.out.println("tableId=" + tableId);
	// tableService.deleteTableById(Integer.parseInt(tableId));
	// Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
	// List<Table> tableList =
	// tableService.getTablesByRestaurantId(restaurant.getId());
	// return tableList;
	// }
	//
	// //新增餐桌
	// @RequestMapping(value="/table_add")
	// @ResponseBody
	// public List<Table> addTable(
	// @RequestBody Table table, HttpSession session){
	//
	// Restaurant restaurant = (Restaurant) session.getAttribute("restaurant");
	//
	// table.setRestaurant(restaurant);
	// table.setTableStatus(0);
	// tableService.createTable(table);
	// List<Table> tableList =
	// tableService.getTablesByRestaurantId(restaurant.getId());
	// return tableList;
	// }

}
